import { _decorator } from 'cc';
import { BaseEvolutionTree } from './BaseEvolutionTree';
import { GameEvents } from '../constants/GameConstants';
import { EventManager } from '../core/EventManager';
import { EvolutionNode } from './EvolutionNode';

const { ccclass, property } = _decorator;

/**
 * 特殊生物进化树
 * 
 * 功能：
 * 1. 定义特殊生物进化路线
 * 2. 管理特殊能力发展
 * 3. 处理独特生存策略
 * 4. 提供变异适应性
 * 
 * 特点：
 * 1. 寄生能力
 * 2. 共生关系
 * 3. 变形能力
 * 4. 特殊适应性
 */
@ccclass('SpecialEvolutionTree')
export class SpecialEvolutionTree extends BaseEvolutionTree {
    onLoad() {
        this.currentStage = 'basic_special';
        this.initEvolutionTree();
    }

    private initEvolutionTree() {
        // 基础特殊生物
        this.addEvolutionNode('basic_special', {
            name: '基础变异体',
            description: '具有基础变异能力的生物',
            requirements: [],
            effects: {
                adaptability: 10,
                mutationRate: 5,
                regeneration: 8
            },
            skills: ['basic_mutation', 'cell_regen']
        });

        // 寄生者路线
        this.addEvolutionNode('parasite', {
            name: '寄生者',
            description: '能够寄生在其他生物体内',
            requirements: ['basic_special'],
            effects: {
                stealthAbility: 15,
                hostControl: 10,
                resourceEfficiency: 12
            },
            skills: ['host_invasion', 'nutrient_drain']
        });

        // 共生者路线
        this.addEvolutionNode('symbiote', {
            name: '共生者',
            description: '与宿主建立互利关系',
            requirements: ['basic_special'],
            effects: {
                bondingStrength: 15,
                mutualBenefit: 12,
                adaptability: 10
            },
            skills: ['symbiotic_bond', 'shared_strength']
        });

        // 变形者路线
        this.addEvolutionNode('shapeshifter', {
            name: '变形者',
            description: '能够改变自身形态',
            requirements: ['basic_special'],
            effects: {
                morphability: 20,
                cellularControl: 15,
                energyEfficiency: 10
            },
            skills: ['shape_change', 'mimicry']
        });

        // 高级寄生体
        this.addEvolutionNode('hive_mind', {
            name: '蜂群意识',
            description: '控制多个宿主形成集群意识',
            requirements: ['parasite'],
            effects: {
                hostControl: 25,
                psychicPower: 20,
                networkStrength: 15
            },
            skills: ['mass_control', 'hive_network']
        });

        // 完美共生体
        this.addEvolutionNode('perfect_symbiote', {
            name: '完美共生体',
            description: '达到与宿主的完美融合',
            requirements: ['symbiote'],
            effects: {
                fusionPower: 25,
                adaptability: 20,
                evolutionSpeed: 15
            },
            skills: ['perfect_fusion', 'evolution_burst']
        });

        // 终极变形者
        this.addEvolutionNode('ultimate_shifter', {
            name: '终极变形者',
            description: '掌握完美的形态转换能力',
            requirements: ['shapeshifter'],
            effects: {
                morphPrecision: 25,
                transformSpeed: 20,
                energyControl: 15
            },
            skills: ['instant_morph', 'perfect_copy']
        });
    }

    public evolve(targetStage: string): boolean {
        if (!this.canEvolve(targetStage)) return false;

        const node = this.evolutionNodes.get(targetStage);
        if (!node) return false;

        // 解锁进化节点
        node.unlock();
        this.currentStage = targetStage;

        // 应用进化效果
        this.applyEvolutionEffects(node);
        this.applySpecialEffects(node);

        // 发送进化完成事件
        EventManager.instance.emit(GameEvents.EVOLUTION_COMPLETE, {
            stage: targetStage,
            effects: node.effects,
            type: 'special'
        });

        return true;
    }

    private applySpecialEffects(node: EvolutionNode) {
        const specialSystem = this.getComponent('SpecialSystem');
        if (!specialSystem) return;

        // 应用特殊能力效果
        if (node.effects.morphability) {
            specialSystem.updateMorphAbility(node.effects.morphability);
        }
        if (node.effects.hostControl) {
            specialSystem.updateHostControl(node.effects.hostControl);
        }
        if (node.effects.bondingStrength) {
            specialSystem.updateBondStrength(node.effects.bondingStrength);
        }

        // 处理特殊状态
        this.handleSpecialStates(node);
    }

    private handleSpecialStates(node: EvolutionNode) {
        const effects = node.effects;

        // 寄生状态
        if (effects.hostControl) {
            this.enableParasiticAbilities(effects.hostControl);
        }

        // 共生状态
        if (effects.bondingStrength) {
            this.enableSymbioticBond(effects.bondingStrength);
        }

        // 变形状态
        if (effects.morphability) {
            this.enableShapeshifting(effects.morphability);
        }
    }

    private enableParasiticAbilities(level: number) {
        const parasiticSystem = this.getComponent('ParasiticSystem');
        if (parasiticSystem) {
            parasiticSystem.setLevel(level);
            parasiticSystem.enableHostControl();
        }
    }

    private enableSymbioticBond(level: number) {
        const symbioticSystem = this.getComponent('SymbioticSystem');
        if (symbioticSystem) {
            symbioticSystem.setLevel(level);
            symbioticSystem.initiateBond();
        }
    }

    private enableShapeshifting(level: number) {
        const morphSystem = this.getComponent('MorphSystem');
        if (morphSystem) {
            morphSystem.setLevel(level);
            morphSystem.enableTransformation();
        }
    }

    // 特殊能力检查
    public canUseSpecialAbility(abilityType: string): boolean {
        const currentNode = this.evolutionNodes.get(this.currentStage);
        if (!currentNode) return false;

        switch (abilityType) {
            case 'parasitic':
                return currentNode.effects.hostControl > 0;
            case 'symbiotic':
                return currentNode.effects.bondingStrength > 0;
            case 'shapeshifting':
                return currentNode.effects.morphability > 0;
            default:
                return false;
        }
    }
} 